package com.yfp.machine.controller;


import cn.hutool.core.bean.BeanUtil;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.machine.annotation.RefreshGeoCache;
import com.yfp.machine.domain.Machine;
import com.yfp.machine.domain.dto.MachineAnalysisDto;
import com.yfp.machine.domain.dto.MachineDto;
import com.yfp.machine.domain.dto.PositionMachine;
import com.yfp.machine.domain.vo.MachineVo;
import com.yfp.machine.service.MachineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 设备管理表控制层
 *
 * 
 * @since 2023-12-11 17:38:53
 */
@Tag(name = "设备管理")
@RestController
@RequestMapping("machine")
public class MachineController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MachineService machineService;

    /**
     * 分页查询所有数据
     *
     * @param machine 查询实体
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有数据")
    @GetMapping
    public TableDataInfo queryByPage(Machine machine, Integer pageNum, Integer pageSize) {
        List<Machine> machines = this.machineService.pagingQueryData(machine);
        TableDataInfo datableByStream = getDatableByStream(machines, pageNum, pageSize);
        List rows = datableByStream.getRows();
        if (null != rows && rows.size() > 0) {
            List<MachineDto> list = rows.stream().map(item -> {
                MachineDto machineDto = new MachineDto();
                BeanUtil.copyProperties(item, machineDto);
                machineService.setMachineDtorelatedData(machineDto);
                return machineDto;
            }).toList();
            datableByStream.setRows(list);
        }
        return datableByStream;
    }

    @GetMapping("onLine")
    @Operation(summary = "设备在线")
    public R onLine(Machine machine) {
        return R.ok(this.machineService.queryOnlineMachine(machine));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询设备详细信息")
    @GetMapping("{id}")
    public R selectOne(@PathVariable @Parameter(name = "设备id", required = true) String id) {
        return R.ok(this.machineService.queryMachineInfoById(id));
    }

    /**
     * 新增数据
     *
     * @param machineVo 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增设备")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @RefreshGeoCache
    @PostMapping
    public R insert(@RequestBody @Valid MachineVo machineVo) {
        return machineService.newAdditionMachine(machineVo);
    }

    /**
     * 修改数据
     *
     * @param machine 实体对象
     * @return 修改结果
     */
    @Operation(summary = "修改设备")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @RefreshGeoCache
    @PostMapping("update")
    public R update(@RequestBody Machine machine) {
        return this.machineService.updateByMachine(machine);
    }

    @Operation(summary = "禁用设备")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @RefreshGeoCache
    @PostMapping("disabled/{id}")
    public R disabled(@PathVariable @Parameter(name = "设备id", required = true) String id) {
        return R.ok(this.machineService.disabledByMachineId(id));
    }

    @Operation(summary = "重启设备")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @GetMapping("restart/{id}")
    public R restart(@PathVariable @Parameter(name = "设备id", required = true) String id) {
        return this.machineService.restartByMachineId(id) ? R.ok() : R.fail();
    }

    @Operation(summary = "通过点位类型ID/设备标签/设备名称/设备编号获取设备列表")
    @GetMapping("list")
    public R<List<PositionMachine>> listByStationId(@Parameter(name = "positionTypeId") String positionTypeId, @Parameter(name = "machineTag") String machineTag, @Parameter(name = "machineName") String machineName, @Parameter(name = "machineId") String machineId, @Parameter(name = "machineType") Integer machineType, @Parameter(name = "cityName") String cityName) {
        return R.ok(this.machineService.machineListByPositionIdOrMachineTag(positionTypeId, machineTag, machineName, machineId, machineType, cityName));
    }


    @Operation(summary = "设备点位分析")
    @GetMapping("machineAnalysis")
    public TableDataInfo machineAnalysis(@Parameter(name = "positionId") String positionId, @Parameter(name = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @Parameter(name = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, @Parameter(name = "cityName") String cityName, Integer pageNum, Integer pageSize) {
        return getDatableByStream(this.machineService.machineAnalysis(positionId, startTime, endTime, cityName), pageNum, pageSize);
    }


    @Log(title = "设备点位分析", businessType = BusinessType.EXPORT)
    @Operation(summary = "设备点位分析导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @Parameter(name = "positionId") String positionId, @Parameter(name = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @Parameter(name = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, @Parameter(name = "cityName") String cityName) {
        List<MachineAnalysisDto> machineAnalysisDtoList = this.machineService.machineAnalysis(positionId, startTime, endTime, cityName);
        ExcelUtil<MachineAnalysisDto> util = new ExcelUtil<>(MachineAnalysisDto.class);
        util.exportExcel(response, machineAnalysisDtoList, "设备点位分析");
    }

    @Operation(summary = "获取企业可关联设备列表")
    @GetMapping("/enterpriseMachine")
    public R<List<PositionMachine>> enterpriseMachine(@Parameter(name = "machineType") Integer machineType) {
        if (null == machineType) {
            return R.ok();
        }
        List<PositionMachine> enterpriseMachine = machineService.findEnterpriseMachine(machineType);
        return R.ok(enterpriseMachine);
    }


    @GetMapping("/playback/{serialNumber}")
    @Operation(summary = "摄像头播放地址")
    public R<String> playback(@PathVariable String serialNumber){
        return machineService.playback(serialNumber);
    }

}

